﻿using System.Security.Cryptography;
using System.Text;

namespace Common
{
    public class DEncrypt
    {
        #region md5加密
        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string ToMd5(string txt)
        {
            // 创建一个MD5对象
            MD5 md5Hasher = MD5.Create();

            // 将载入的文本转化为字节组
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(txt));

            // 创建一个新的stringbuilder来存放字节组
            // 创建输出文本
            StringBuilder sBuilder = new StringBuilder();

            // 将字节数组放入文本中
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();

        }
        #endregion
    }
}
